package com.jk.myimkit.imkit.widget.provide;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jk.myimkit.R;
import com.jk.myimkit.imkit.model.ProviderTag;
import com.jk.myimkit.imkit.model.UIMessage;
import com.jk.myimkit.imkit.widget.AsyncImageView;

import java.text.SimpleDateFormat;
import java.util.Date;

import io.rong.message.PublicServiceRichContentMessage;

@ProviderTag(
        messageContent = PublicServiceRichContentMessage.class,
        showPortrait = false,
        centerInHorizontal = true,
        showSummaryWithName = false
)
public class PublicServiceRichContentMessageProvider extends IContainerItemProvider.MessageProvider<PublicServiceRichContentMessage> {
    private int width;
    private int height;

    public PublicServiceRichContentMessageProvider() {
    }

    public View newView(Context context, ViewGroup group) {
        PublicServiceRichContentMessageProvider.ViewHolder holder = new PublicServiceRichContentMessageProvider.ViewHolder();
        View view = LayoutInflater.from(context).inflate(R.layout.rc_item_public_service_rich_content_message, (ViewGroup)null);
        holder.title = (TextView)view.findViewById(R.id.rc_title);
        holder.time = (TextView)view.findViewById(R.id.rc_time);
        holder.description = (TextView)view.findViewById(R.id.rc_content);
        holder.imageView = (AsyncImageView)view.findViewById(R.id.rc_img);
        WindowManager m = (WindowManager)view.getContext().getSystemService(Context.WINDOW_SERVICE);
        int w = m.getDefaultDisplay().getWidth() - 35;
        view.setLayoutParams(new LinearLayout.LayoutParams(w, -2));
        this.width = w - 100;
        this.height = 800;
        view.setTag(holder);
        return view;
    }

    public void bindView(View v, int position, PublicServiceRichContentMessage content, UIMessage message) {
        PublicServiceRichContentMessageProvider.ViewHolder holder = (PublicServiceRichContentMessageProvider.ViewHolder)v.getTag();
        PublicServiceRichContentMessage msg = (PublicServiceRichContentMessage)message.getContent();
        if (msg.getMessage() != null) {
            holder.title.setText(msg.getMessage().getTitle());
            holder.description.setText(msg.getMessage().getDigest());
            holder.imageView.setResource(msg.getMessage().getImageUrl(), 0);
        }

        String time = this.formatDate(message.getReceivedTime(), "MM月dd日 HH:mm");
        holder.time.setText(time);
    }

    private String formatDate(long timeMillis, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(timeMillis));
    }

    public Spannable getContentSummary(PublicServiceRichContentMessage data) {
        return null;
    }

    public Spannable getContentSummary(Context context, PublicServiceRichContentMessage data) {
        return new SpannableString(data.getMessage().getTitle());
    }

    public void onItemClick(View view, int position, PublicServiceRichContentMessage content, UIMessage message) {
        String url = content.getMessage().getUrl();
        String action = "io.rong.imkit.intent.action.webview";
        Intent intent = new Intent(action);
        intent.setPackage(view.getContext().getPackageName());
        intent.putExtra("url", url);
        view.getContext().startActivity(intent);
    }

    private static class ViewHolder {
        TextView title;
        AsyncImageView imageView;
        TextView time;
        TextView description;

        private ViewHolder() {
        }
    }
}
